package x.ovo.wechat.bot;

import lombok.extern.slf4j.Slf4j;
import x.ovo.wechat.bot.common.command.Command;
import x.ovo.wechat.bot.common.command.CommandExecutor;
import x.ovo.wechat.bot.common.entity.message.TextMessage;
import x.ovo.wechat.bot.common.plugin.Plugin;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("unchecked")
@Slf4j(topic = "表情包助手")
public class ComeDogCommandExecutor extends CommandExecutor {
    protected ComeDogCommandExecutor(Plugin plugin) {
        super(plugin);
    }

    @Override
    public void execute(Command command, TextMessage message) {
        String fromName = command.getFromName();
        String[] args = command.formatArgs();

        if (!inAuthors(fromName)) {
            this.context.getApi().sendText(message.getFromUser().getUserName(), "你没有权限执行此命令，不要调皮");
            return;
        }

        try {
            if (args.length == 0 || args[0].equals("help")) {
                this.context.getApi().sendText(message.getFromUser().getUserName(), help());
            } else if (args[0].equals("add")) {
                this.context.getApi().sendText(message.getFromUser().getUserName(), add(Arrays.copyOfRange(args, 1, args.length)));
            } else if (args[0].equals("remove")) {
                this.context.getApi().sendText(message.getFromUser().getUserName(), remove(args[1]));
            } else if (args[0].equals("list")) {
                this.context.getApi().sendText(message.getFromUser().getUserName(), list());
            }else {
                this.context.getApi().sendText(message.getFromUser().getUserName(), "未知命令");
            }
        } catch (Exception e) {
            log.error("命令执行失败", e);
            this.context.getApi().sendText(message.getFromUser().getUserName(), "命令执行失败" + e.getMessage());
        }
    }

    private String help() {
        return """
                [表情包助手]插件帮助：
                help                        显示帮助
                add name md5 keyword...     添加用户
                remove name                 移除用户
                list                        显示
                """;
    }

    private String add(String... args) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", args[0]);
        map.put("md5", args[1]);
        map.put("keyword", Arrays.asList(Arrays.copyOfRange(args, 2, args.length)));
        List<Map<String, Object>> data = (List<Map<String, Object>>) this.plugin.getConfig().getList("data");
        data.forEach(map1 -> {
            if (map1.get("name").equals(args[0])) {
                data.remove(map1);
            }
        });
        data.add(map);
        this.plugin.saveConfig();
        return "添加 " + args[0] + " 成功";
    }

    private String remove(String... names) {
        List<Map<String, Object>> data = (List<Map<String, Object>>) this.plugin.getConfig().getList("data");
        data.forEach(map -> {
            if (Arrays.asList(names).contains(map.get("name").toString())) {
                data.remove(map);
            }
        });
        this.plugin.saveConfig();
        return "移除 " + Arrays.toString(names) + " 成功";
    }

    private String list() {
        List<Map<String, Object>> data = (List<Map<String, Object>>) this.plugin.getConfig().getList("data");
        StringBuilder sb = new StringBuilder();
        data.forEach(map -> sb.append(map.get("name")).append(" ").append(map.get("md5")).append(" ").append(map.get("keyword")).append("\n"));
        return "[表情包助手] 现有命令：\n" + sb;
    }
}
